package com.danny.Controller;

import com.danny.mapper.ItemMapper;
import com.danny.pojo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ItemController {
    @Autowired
    private ItemMapper itemMapper;

    @GetMapping("/queryAllItems")
    public List<Item> queryAllItems(){
        return itemMapper.queryAllItems();
    }

    @GetMapping("/queryItemsLimit")
    public List<Item> queryItemsLimit(int page, int pageSize){
        Map<String,Object> map =new HashMap<>();
        map.put("page",page);
        map.put("pageSize",pageSize);
        return itemMapper.queryItemsLimit(map);
    }

    @GetMapping("/queryItemsLimitHasInput")
    public List<Item>queryItemsLimitHasInput(Item item, int page, int pageSize){
        Map<String,Object> map =new HashMap<>();
        map.put("item",item);
        map.put("page",page);
        map.put("pageSize",pageSize);
        return itemMapper.queryItemsLimitHasInput(map);
    }

    /*
    @GetMapping("/queryItemsLimitByType")
    public List<Item>queryItemsLimitByType(String itemType, int page, int pageSize){
        Map<String,Object> map =new HashMap<>();
        map.put("itemType",itemType);
        map.put("page",page);
        map.put("pageSize",pageSize);
        return itemMapper.queryItemsLimitByType(map);
    }
     */

    @GetMapping("/itemsCount")
    public int itemsCount(){
        return itemMapper.itemsCount();
    }

    @GetMapping("/itemsCountHasInput")
    public int itemsCountHasInput(String itemInfo){
        return itemMapper.itemsCountHasInput(itemInfo);
    }

    @GetMapping("/addItem")
    public String addItem(Item item){
        List<Item> list = queryAllItems();
        for (Item f:
                list) {
            if(item.getItemName().equals(f.getItemName()))
                return "repetitive";
        }
        if(itemMapper.addItem(item)==0){
            return "error";
        }
        return "inserted";
    }

    @GetMapping("/updateItem")
    public String updateItem(Item item){
        if(itemMapper.updateItem(item)==0){
            return "error";
        }
        return "updated";
    }

    @GetMapping("/deleteItemById")
    public String deleteItemById(int id){
        if(itemMapper.deleteItemById(id)==0){
            return "error";
        }
        return "deleted";
    }
}
